找传奇、传世资源到传世资源站!

形态学滤波(基于opencv4.4)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

基于opencv4.4的形态学滤波

#include<opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace cv;using namespace std;Mat srcImage, dstImage;int g_nElementShape = MORPH_RECT;//内核形状为矩形int g_nMaxIterationNum = 10;int g_OpenCloseNum = 0;int g_TopBlackHatNum = 0;void on_OpenClose(int, void*){Mat element = getStructuringElement(g_nElementShape, Size(g_nMaxIterationNum * 2 1, g_nMaxIterationNum * 2 1), Point(g_nMaxIterationNum, g_nMaxIterationNum));if (g_OpenCloseNum==0){morphologyEx(srcImage, dstImage, MORPH_OPEN, element);}else{morphologyEx(srcImage, dstImage, MORPH_CLOSE, element);}imshow("开/闭运算", dstImage);}void on_TopBlackHat(int, void*){Mat element = getStructuringElement(g_nElementShape, Size(g_nMaxIterationNum * 2 1, g_nMaxIterationNum * 2 1), Point(g_nMaxIterationNum, g_nMaxIterationNum));if (g_TopBlackHatNum == 0){morphologyEx(srcImage, dstImage, MORPH_TOPHAT, element);}else{morphologyEx(srcImage, dstImage, MORPH_BLACKHAT, element);}imshow("顶/黑帽运算", dstImage);}int main(){system("color 5E");srcImage = imread("C:\\Users\\ADMIN\\Desktop\\图片\\图片1.jpg");if (!srcImage.data){printf("读取图片失败,请检查路径是否正确 \n");return false;}namedWindow("原始图");imshow("原始图", srcImage);//开闭运算namedWindow("开/闭运算",1);createTrackbar("开/闭", "开/闭运算", &g_OpenCloseNum, 1, on_OpenClose);on_OpenClose(g_OpenCloseNum, 0);createTrackbar("内核值", "开/闭运算", &g_nMaxIterationNum, g_nMaxIterationNum*2 1, on_OpenClose);on_OpenClose(g_nMaxIterationNum, 0);//顶黑帽运算namedWindow("顶/黑帽运算", 1);createTrackbar("顶/黑帽", "顶/黑帽运算", &g_TopBlackHatNum, 1, on_TopBlackHat);createTrackbar("内核值", "顶/黑帽运算", &g_nMaxIterationNum, g_nMaxIterationNum * 2 1, on_TopBlackHat);while (1){int c;//执行回调函数on_TopBlackHat(g_OpenCloseNum, 0);on_TopBlackHat(g_nMaxIterationNum, 0);//获取按键c = waitKey(0);//按下键盘Q或者Esc,程序退出if ((char)c == 'q' || (char)c == 27)break;//按下键盘按键1,使用椭圆(Elliptic)结构元素MORPH_ELLIPSEif ((char)c == 49 ){g_nElementShape = MORPH_ELLIPSE;}//按下键盘按键2,使用椭圆(Rectangle)结构元素MORPH_RECTelse if ((char)c == 50){g_nElementShape = MORPH_RECT;}//按下键盘按键3,使用十字形(Cross-shaped)结构元素MORPH_CROSSelse if ((char)c == 51){g_nElementShape = MORPH_CROSS;}//按下键盘按键3,使用十字形(Cross-shaped)结构元素MORPH_CROSSelse if ((char)c == 50){g_nElementShape = MORPH_CROSS;}}return 0;}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复